package com.example.vv.model.home;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.vv.R;
import com.example.vv.frameWork.entity.HomeEntity;
import com.example.vv.frameWork.mvp.MvpFragment;
import com.example.vv.frameWork.view.ChatDetailItemDecoration;
import com.example.vv.frameWork.view.CustomeTitleBar;
import com.example.vv.model.home.base.adapter.HomeAdapter;
import com.example.vv.model.home.base.adapter.MyLoader;
import com.example.vv.model.home.base.contract.HomeContract;
import com.example.vv.model.home.base.presenter.HomePresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.youth.banner.Banner;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class HomeFragment extends MvpFragment<HomeContract.View, HomePresenter> implements HomeContract.View {
    @BindView(R.id.home_title)
    CustomeTitleBar homeTitle;
    @BindView(R.id.home_banner)
    Banner homeBanner;
    @BindView(R.id.home_recycler)
    RecyclerView homeRecycler;
    @BindView(R.id.home_smart)
    SmartRefreshLayout homeSmart;
    private HomeAdapter homeAdapter;

    @Override
    public int initLayout() {
        return R.layout.fragment_home;
    }

    @Override
    public HomePresenter initPresenter() {
        return new HomePresenter();
    }

    @Override
    public void initView() {
        homeTitle.getLeft_img().setVisibility(View.INVISIBLE);
        homeTitle.getRight_rl().setVisibility(View.VISIBLE);
        homeSmart.setRefreshHeader(new ClassicsHeader(getMContext()).setSpinnerStyle(SpinnerStyle.FixedBehind).setPrimaryColorId(R.color.c_ffffff).setAccentColorId(R.color.c_ff3c88));
        homeSmart.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                homeSmart.finishLoadMore(2000);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                homeSmart.finishRefresh(2000);
            }
        });
        homeRecycler.setLayoutManager(new GridLayoutManager(getMContext(),3));
        homeRecycler.addItemDecoration(new ChatDetailItemDecoration(32,20));
    }

    @Override
    public void initData() {
        homeSmart.autoRefresh();
        homeAdapter = new HomeAdapter(getMContext());
        homeRecycler.setAdapter(homeAdapter);
        ArrayList<String> imageViews = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            imageViews.add("");
        }
        //设置轮播的动画效果，内含多种特效，可点入方法内查找后内逐一体验
        homeBanner.setBannerAnimation(Transformer.Default);
        //设置图片加载器，图片加载器在下方
        homeBanner.setImageLoader(new MyLoader()).
                setImages(imageViews).
                start();

    }

    @Override
    public void doBusiness() {
        List<HomeEntity.Song> songList=new ArrayList<>();
        for (int i = 0; i <10 ; i++) {
            songList.add(new HomeEntity.Song());
        }
        homeAdapter.setMyData(songList);
    }

    @Override
    public View titleBar() {
        return homeTitle;
    }

    @Override
    public Context getMContext() {
        return getContext();
    }
}
